using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;
using Common.Utilities.NetworkUtilities;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FileLoopEditor : Form,IComponentEditor 
    {
        private object ownerActivityNode;
        private FileEnumeatorComponent fileLoopComponent;

        /// <summary>
        /// 
        /// </summary>
        public FileLoopEditor()
        {
            InitializeComponent();
        }

        private void FileLoopEditor_Load(object sender, EventArgs e)
        {

        }

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return ownerActivityNode; }
            set { ownerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(FileEnumeatorComponent); }
        }

        public void LoadSettings(
            GlobalVariable[] globalVars,
            WorkflowComponentBase[] fromComponents, 
            object activityNode, WorkflowComponentBase wfComponent)
        {
            this.ownerActivityNode = activityNode;
            this.fileLoopComponent = (FileEnumeatorComponent) wfComponent;
            this.txt_FolderPath.Text = this.fileLoopComponent.FolderPath;
            this.txt_FileNamePattern.Text = this.fileLoopComponent.FileNamePattern;
        }

        public TestResult Test()
        {
            return new TestResult(true, string.Empty);
        }

        public void SaveSettings()
        {
            this.fileLoopComponent.FolderPath = this.txt_FolderPath.Text;
            this.fileLoopComponent.FileNamePattern = this.txt_FileNamePattern.Text;
        }

        private void btn_Browse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg=new FolderBrowserDialog();
            if(dlg.ShowDialog()==DialogResult.OK)
            {
                this.txt_FolderPath.Text = NetworkShareMap.getUNCPath(dlg.SelectedPath);
            }
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            this.lb_FileNames.Items.Clear();
            if(Directory.Exists(this.fileLoopComponent.FolderPath))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(this.fileLoopComponent.FolderPath);
                List<string> fileNames=new List<string>();
                FileInfo[] files = dirInfo.GetFiles();
                if (!string.IsNullOrEmpty(this.fileLoopComponent.FileNamePattern))
                {
                    Regex regex = new Regex(this.fileLoopComponent.FileNamePattern, RegexOptions.IgnoreCase);
                    Match match;
                    foreach (FileInfo fileInfo in files)
                    {
                        match = regex.Match(fileInfo.Name);
                        if(match.Success)
                        {
                            fileNames.Add(fileInfo.Name);
                        }
                    }
                }
                else
                {
                    foreach(FileInfo fileInfo in files)
                    {
                        fileNames.Add(fileInfo.Name);
                    }
                }
                if(fileNames.Count>0)
                {
                    foreach(string fileName in fileNames)
                    {
                        this.lb_FileNames.Items.Add(fileName);
                    }
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this.ownerActivityNode, this.fileLoopComponent);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}